<?php


namespace app\admin\controller;

use app\common\library\enum\CodeEnum;
use app\common\library\enum\OrderStatusEnum;

class Orders extends BaseAdmin
{

    /**
     * 订单列表
     *
     *
     * @return mixed
     */
    public function index()
    {
        $this->assign($this->logicOrders->getOrdersAllStat());
        $this->assign('code', $this->logicPay->getCodeList());
        $this->assign('channel', $this->logicPay->getChannelList());
        return $this->fetch();
    }

    private function getSearchParam()
    {
        $where = [];

        //状态
        is_numeric($this->request->param('status')) && $where['a.status']
            = ['eq', $this->request->param('status')];

        //组合搜索
        !empty($this->request->param('trade_no')) && $where['a.trade_no|a.out_trade_no']
            = ['like', '%' . $this->request->param('trade_no') . '%'];

        !empty($this->request->param('uid')) && $where['a.uid']
            = ['eq', $this->request->param('uid')];

        !empty($this->request->param('uidname')) && $where['u.username']
            = ['like', '%' .$this->request->param('uidname'). '%'];

        !empty($this->request->param('channel')) && $where['a.channel']
            = ['eq', $this->request->param('channel')];

        !empty($this->request->param('channelrel')) && $where['d.id']
            = ['eq', $this->request->param('channelrel')];

        //时间搜索  时间戳搜素
        $where['a.create_time'] = $this->parseRequestDate();

        //range
        $dateRange = $this->request->param('range');
        if (!empty($dateRange)) {
            $dateStart = substr($dateRange, 0, 10);
            $dateEnd = substr($dateRange, -10);

            $where['a.create_time'] = ['between', [strtotime($dateStart), strtotime("+1 day", strtotime($dateEnd))]];
        }

        return $where;
    }

    /**
     * 交易统计
     *
     *
     */
    public function getStat()
    {
        $where =$this->getSearchParam();

        $data = $this->logicOrders->getOrderStatNew($where);

        $this->result(1,'', current($data));
    }

    /**
     * 交易列表
     *
     *
     */
    public function getList()
    {
        $where =$this->getSearchParam();

        $data = $this->logicOrders->getOrderListNew($where, true, 'a.create_time desc', $this->request->param('limit'));

        $this->result(
            $data && !empty($data) && $data->total() > 0 ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $data->total(),
                    'data' => $data->items()
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => 0,
                    'data' => []
                ]
        );
    }

    /**
     * 获取详情
     * 1.基本
     * 2.回调
     * 3.商户
     * 4.结算
     *
     *
     * @return mixed
     */
    public function details()
    {
        $where['a.id'] = $this->request->param('id', '0');

        $data = $this->logicOrders->getOrderListNew($where, true, 'a.create_time desc', 1);

        //订单
        $order = $data[0]; // $this->logicOrders->getOrderInfo($where);

        $notify = [];
        //当支付成功的时候才会看有没有回调成功
        if ($order['status'] == '2') {
            //回调
            // $notify = $this->logicOrders->getOrderNotify(['order_id'=> $where['id']]);
        }

        $this->assign('order', $order);
        $this->assign('notify', $notify);

        return $this->fetch();
    }


    /**
     * 退款列表
     *
     *
     * @return mixed
     */
    public function refund()
    {
        $this->assign('code', $this->logicPay->getCodeList());
        return $this->fetch();
    }


    /**
     * 商户订单统计
     *
     *
     */
    public function user()
    {
        return $this->fetch();
    }

    /**
     * 商户交易统计
     *
     *
     */
    public function userList()
    {
        $where = [];
        //组合搜索
        !empty($this->request->param('uid')) && $where['uid']
            = ['eq', $this->request->param('uid')];

        //时间搜索  时间戳搜素
        $where['create_time'] = $this->parseRequestDate();

        $data = $this->logicOrders->getOrderUserStat($where);

        $count = count($data);

        $this->result(
            $data || !empty($data) ?
                [
                    'code' => CodeEnum::SUCCESS,
                    'msg' => '',
                    'count' => $count,
                    'data' => $data
                ] : [
                    'code' => CodeEnum::ERROR,
                    'msg' => '暂无数据',
                    'count' => $count,
                    'data' => $data
                ]
        );
    }

    /**
     * 商户渠道统计
     *
     *
     */
    public function channel()
    {
        $this->assign('channel', $this->logicPay->getChannelList());
        return $this->fetch();
    }

    /**
     * 商户渠道统计
     *
     *
     */
    public function channelList()
    {
        $where = [];
        //组合搜索
        !empty($this->request->param('cnl_id')) && $where['a.cnl_id']
            = ['eq', $this->request->param('cnl_id')];

        //时间搜索  时间戳搜素
        $where['a.create_time'] = $this->parseRequestDate();

        $data = $this->logicOrders->getOrderChannelStat($where);
        $this->result(CodeEnum::SUCCESS, '', $data);
    }

    /**
     * 这里还是写入队列
     *
     *
     *
     */
    public function subnotify()
    {
        $this->result($this->logicOrders->pushOrderNotify($this->request->param('order_id')));
    }
}
